home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / fzoom.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  132 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    fzoom- 
  19.  *        Magnify or minify an array of longs using impulse zoom
  20.  *        with no filtering.
  21.  *
  22.  *                     Paul Haeberli - 1992
  23.  *
  24.  */
  25. #include "math.h"
  26. #include "stdio.h"
  27.  
  28. #define GRIDTOFLOAT(pos,n)    (((pos)+0.5)/(n))
  29. #define FLOATTOGRID(pos,n)    ((pos)*(n))
  30.  
  31. static int zanx, zbnx;
  32. static unsigned long **zmap;
  33. static int zptrs;
  34.  
  35. static void makemap(int anx, int bnx)
  36. {
  37.     int n, x, y;
  38.     unsigned long **zmapptr;
  39.     unsigned long *ybase;
  40.     float fx, fy;
  41.     int ax, ay;
  42.  
  43.     if(bnx>zptrs) {
  44.     if(zmap)
  45.         free(zmap);
  46.     zmap = (unsigned long **)malloc(bnx*sizeof(unsigned long *));
  47.       zptrs = bnx;
  48.     }
  49.     zmapptr = zmap;
  50.     for(x=0; x<bnx; x++) {
  51.     fx = GRIDTOFLOAT(x,bnx);
  52.     ax = FLOATTOGRID(fx,anx);
  53.     *zmapptr++ = (unsigned long *)(ax<<2);
  54.     }
  55.     zanx = anx;
  56.     zbnx = bnx;
  57. }
  58.  
  59. /*
  60.  *    general zoom follows
  61.  *
  62.  */
  63. void
  64. fastzoom(unsigned long* abuf, unsigned long* bbuf, 
  65.                 int anx, int any, int bnx, int bny)
  66. {
  67.     unsigned long **zmapptr;
  68.     unsigned long *dptr;
  69.     int x, y, base, ay;
  70.     float fy;
  71.  
  72.     if(zanx != anx || zbnx != bnx) 
  73.     makemap(anx,bnx);
  74.     dptr = bbuf;
  75.     y = bny;
  76.     for(y=0; y<bny; y++) {
  77.         x = bnx;
  78.     zmapptr = zmap;
  79.     fy = GRIDTOFLOAT(y,bny);
  80.     ay = FLOATTOGRID(fy,any);
  81.     base = (int)(abuf+(ay*anx));
  82.         while(x>=8) {
  83.         dptr[0] = *(long*)(base+(unsigned int)(zmapptr[0]));
  84.         dptr[1] = *(long*)(base+(unsigned int)(zmapptr[1]));
  85.         dptr[2] = *(long*)(base+(unsigned int)(zmapptr[2]));
  86.         dptr[3] = *(long*)(base+(unsigned int)(zmapptr[3]));
  87.         dptr[4] = *(long*)(base+(unsigned int)(zmapptr[4]));
  88.         dptr[5] = *(long*)(base+(unsigned int)(zmapptr[5]));
  89.         dptr[6] = *(long*)(base+(unsigned int)(zmapptr[6]));
  90.         dptr[7] = *(long*)(base+(unsigned int)(zmapptr[7]));
  91.         dptr += 8;
  92.         zmapptr += 8;
  93.         x -= 8;
  94.     }
  95.     while(x--) 
  96.         *dptr++ = *(long*)(base+(unsigned int)(*zmapptr++));
  97.     }
  98. }
  99.  
  100. #ifdef TESTIT
  101. float gettime();
  102.  
  103. main(argc,argv)
  104. int argc;
  105. char **argv;
  106. {
  107.     int anx, any;
  108.     int bnx, bny;
  109.     unsigned long *abuf, *bbuf;
  110.     float sc;
  111.     int i;
  112.  
  113.     sc = atof(argv[1]);
  114.     sizeofimage("in.rgb",&anx,&any);
  115.     abuf = (unsigned long *)longimagedata("in.rgb");
  116.     bnx = sc*anx;
  117.     bny = sc*any;
  118.     bbuf = (unsigned long *)malloc(bnx*bny*sizeof(long));
  119.     fastzoom(abuf,bbuf,anx,any,bnx,bny);
  120.  
  121.     sleep(1);
  122.     cleartime();
  123.     for(i=0; i<50; i++) 
  124.         fastzoom(abuf,bbuf,anx,any,bnx,bny);
  125.     printf("time is %f\n",gettime());
  126.  
  127.     longstoimage(bbuf,bnx,bny,3,"out.rgb");
  128.     system("istat out.rgb");
  129.     system("ipaste out.rgb");
  130. }
  131. #endif
  132.